/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pgentity;

import config.PGConfig;
import db.DBContext;
import java.util.HashMap;
import java.util.Map;
import pgentity.pool.EntityFactory;
import pgentity.services.EggStoreServices;
import db.PGKeys;
import share.PGMacro;
import db.RedisKey;
import share.PGHelper;

/**
 *
 * @author KieuAnh
 */
@EntityFactory(factorier = "loadInventory")
public class Inventory implements PGEntity
{
    private final String uid;
    private final RedisKey redisKey;
    private final EggStore eggs;
    private int level;
    
    private Inventory(String uid)
    {
        this.uid = uid;
        this.redisKey = redisKey(uid);
        this.eggs = EggStore.getEggStore(redisKey.getChild(PGKeys.FD_EGGS));
    }
    
    public static EggStore getInvEggStore(String uid)
    {
        return EggStore.getEggStore(redisKey(uid).getChild(PGKeys.FD_EGGS));
    }
    
    public static Inventory getInventory(String uid)
    {
        return loadInventory(uid);
//        return EntityPool.inst().get(Inventory.class, uid);
    }
    
    private static Inventory loadInventory(String uid)
    {
        Inventory inventory = new Inventory(uid);
        inventory.updateFromDB();
        
        return inventory;
    }
    
    public static void destroy(String uid)
    {
        RedisKey rKey = redisKey(uid);
        DBContext.Redis().del(rKey);
        EggStore.destroyEggStore(rKey.getChild(PGKeys.FD_EGGS));
        
//        EntityPool.inst().remove(Inventory.class, uid);
    }
    
    public static RedisKey redisKey(String uid)
    {
        return User.redisKey(uid).getChild(PGKeys.FD_INVENTORY);
    }
    
    @Override
    public void updateFromDB()
    {
        this.level = PGHelper.toInt(DBContext.Redis()
            .hget(redisKey, PGMacro.LEVEL));
    }

    @Override
    public void saveToDB()
    {
        DBContext.Redis().hset(redisKey, PGMacro.LEVEL, String.valueOf(level));
    }
    
    public Map<String, Object> buildAMF()
    {
        Map<String, Object> data = new HashMap(2);
        data.put(PGMacro.LEVEL, level);
        data.put(PGMacro.EGGS,
                EggStoreServices.inst().buildAMF(this.eggStore()));
        
        return data;
    }
    
    //<editor-fold defaultstate="collapsed" desc="Properties">
    public String getUID()
    {
        return this.uid;
    }
    
    public EggStore eggStore()
    {
        return this.eggs;
    }
    
    public int numberItems()
    {
        return this.eggs.nOfEggs();
    }

    public int getLevel() {
        return level;
    }
    
    public int setLevel(int lvl)
    {
        this.level = lvl;
        return lvl;
    }
    
    public int getMax()
    {
        return PGConfig.inst().getUser().getInventory().get(level).getSize();
    }
    //</editor-fold>

    public Object dump()
    {
        Map<String, Object> data = new HashMap(2);
        data.put(PGMacro.DATA, DBContext.Redis().hgetall(redisKey));
        data.put(PGMacro.EGGS, this.eggs.dump());
        
        return data;
    }
}
